//
//  RGFrendsContacts.m
//  RedGrass
//
//  Created by wukong on 2019/2/23.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGFrendsContacts.h"
#import <AddressBookUI/AddressBookUI.h>

@implementation RGFrendsContacts

+ (void)tryToGetAllContactsSuccess:(void (^)(NSArray *data))success failed:(void (^)(void))failed{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 1、获取通讯录信息
        ABAddressBookRef addressBooks = nil;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
            addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
            dispatch_semaphore_t sema = dispatch_semaphore_create(0);
            ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){
                dispatch_semaphore_signal(sema);
            });
            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        }
        else {
            addressBooks = ABAddressBookCreate();
        }
        
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
        CFRelease(addressBooks);
        if (allPeople == nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                failed();
                if(allPeople != nil)
                {
                    CFRelease(allPeople);
                }
            });
        }else{
            // 3、格式转换
            NSMutableArray *data = [[NSMutableArray alloc] init];
            for (NSInteger i = 0; i < nPeople; i++) {
                ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
                ABPropertyID multiProperties[] = {
                    kABPersonPhoneProperty,
                    kABPersonEmailProperty
                };
                NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
                for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
                    ABPropertyID property = multiProperties[j];
                    ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
                    NSInteger valuesCount = 0;
                    if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
                    if (valuesCount == 0) {
                        if (valuesRef != nil) {
                            CFRelease(valuesRef);
                        }
                        continue;
                    }
                    for (NSInteger k = 0; k < valuesCount; k++) {
                        CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
                        switch (j) {
                            case 0: {// Phone number
                                NSString *phone = (__bridge NSString*)value;
                                if(phone.length){
                                    [data addObject:phone];
                                }
                                break;
                            }
                        }
                        CFRelease(value);
                    }
                    CFRelease(valuesRef);
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                success(data);
            });
        }
    });
}


@end
